package com.neu.flashmall.product.controller;

import com.neu.common.utils.Result;
import com.neu.flashmall.product.entity.PmsSpuInfo;
import com.neu.flashmall.product.service.PmsSpuInfoService;
import com.neu.flashmall.product.vo.FatherGoodVo;
import com.neu.flashmall.product.vo.FilterCondition;
import com.neu.flashmall.product.vo.RecommendSpuListVo;
import com.neu.flashmall.product.vo.SpuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/pms_spu")
@Api(value = "")
public class PmsSpuInfoController {
    @Resource
    PmsSpuInfoService pmsSpuInfoService;

    @GetMapping(value = "/getPaged")
    @ApiOperation(value = "获取所有的spuinfo信息")
    public Result getAllCategory(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "3") int pageSize ){
        return Result.ok().put(pmsSpuInfoService.getPaged(pageNum,pageSize));
    }

    @PostMapping(value = "/changePublishStatus")
    @ApiOperation(value = "更改上架状态")
    public Result changePublishStatus(@RequestBody long id ){
        String mes = pmsSpuInfoService.changePublishStatus(id);
        return Result.ok().setMessage(mes);
    }

    @PostMapping(value = "/check/insert")
    @ApiOperation(value = "添加SpuInfo")
    public Result addCategoryLevel1(@RequestBody PmsSpuInfo pmsSpuInfo){
        String mes = pmsSpuInfoService.insert(pmsSpuInfo);
        if(mes.equals("添加成功")){
            return Result.ok().setMessage(mes);
        }
        return Result.error().setMessage(mes);
    }

    @PostMapping(value = "/check/update")
    @ApiOperation(value = "更新SpuInfo")
    public Result updateSpuInfo(@RequestBody PmsSpuInfo pmsSpuInfo){
        String mes = pmsSpuInfoService.updateByPrimaryKey(pmsSpuInfo);
        if(mes.equals("修改成功")){
            return Result.ok().setMessage(mes);
        }
        return Result.error().setMessage(mes);
    }

    @DeleteMapping(value = "/check/deleteSpu")
    @ApiOperation(value = "删除SpuInfo")
    public Result deleteSpuInfo(Long id){
        String mes = pmsSpuInfoService.deleteByPrimaryKey(id);
        if(mes.equals("删除成功")){
            return Result.ok().setMessage(mes);
        }
        return Result.error().setMessage(mes);
    }

    @GetMapping("/getOneById")
    public Result getOneById(Long id){
        PmsSpuInfo result = pmsSpuInfoService.getOne(id);
        return Result.ok().put(result);
    }

    @GetMapping("/getSpuInfoById")
    public Result getSpuInfoById(Long id){
        PmsSpuInfo result = pmsSpuInfoService.getOne(id);
        return Result.ok().put(result);
    }

    @GetMapping("/getRecommendBySecondCategory")
    public Result getRecommendByCategory(Long categoryId){
        RecommendSpuListVo result = pmsSpuInfoService.getRecommendSpuListByCategory(categoryId);
        return Result.ok().put(result);
    }

    @PostMapping("/getFilterSpu")
    public Result getFilterSpu(@RequestBody FilterCondition condition){
        List<SpuVo> allSpu = this.pmsSpuInfoService.getFilterSpu(condition);
        return Result.ok().put(allSpu);
    }

    @GetMapping("/getByCategoryId")
    public Result getByCategoryId(Long categoryId,Long pageSize,Long pageNum){
        List<SpuVo> result = this.pmsSpuInfoService.getByCategoryId(categoryId,pageSize,pageNum);
        return Result.ok().put(result);
    }

    @GetMapping("/getFatherGood")
    public Result getFatherGood(Long id){
        FatherGoodVo fatherGoodVo = this.pmsSpuInfoService.getFatherGoodVo(id);
        return Result.ok().put(fatherGoodVo);
    }

    @PostMapping("/getByKeyWord")
    public Result getByKeyWord(String keyWord){
        List<SpuVo> spuVoList = pmsSpuInfoService.getByKeyWord(keyWord);
        return Result.ok().put(spuVoList);
    }
}
